package registnet.records.summary.freq.source;

import java.util.List;

import registnet.records.College;
import registnet.records.DefaultCourse;
import registnet.records.summary.freq.GradeFrequency;
import resources.Constants;

public class GradeFrequencySource {

	private College college;
	private DefaultCourse course;
	private GradeFrequency gradeFrequency;
	private int semester;
	private int year;
	
	public GradeFrequencySource(DefaultCourse course, int semester, int year){
		this.course = course;
		this.semester = semester;
		this.year = year;
		this.load();
	}
	
	public GradeFrequencySource(College college, DefaultCourse course, int semester, int year){
		this.course = course;
		this.college = college;
		this.semester = semester;
		this.year = year;
		this.load();
	}
	
	
	public void load(){
		registnet.dao.GradePeer gp = new registnet.dao.GradePeer();
		registnet.dao.Course DAOCourse = new registnet.dao.CoursePeer().getCourse(this.course.getCourseCode());
		List<registnet.dao.Grade> DAOGrades;
		if (this.college == null){
			DAOGrades = gp.getGradesByCourse(DAOCourse.getCourseId(), this.semester, this.year);
			this.gradeFrequency = new GradeFrequency(this.college, this.course, this.semester, this.year);
		}
		else{
			DAOGrades = gp.getGradesByCourse(new registnet.dao.CollegePeer().getCollegeId(this.college.getCollegeCode()), DAOCourse.getCourseId(), this.semester, this.year);
			this.gradeFrequency = new GradeFrequency(this.college, this.course, this.semester, this.year);
		}
		
		for(int i=0; i<DAOGrades.size(); i++){
			registnet.dao.Grade DAOGrade = DAOGrades.get(i);
			this.gradeFrequency.setFrequency(Constants.MAP_GRADE_VALUE.get(DAOGrade.getGrade()), this.gradeFrequency.getFrequency(Constants.MAP_GRADE_VALUE.get(DAOGrade.getGrade())) + 1);
		}
		
	}
	
	public GradeFrequency getGradeFrequency(){
		return this.gradeFrequency;
	}
}
